<?php
namespace App\Services;

use App\Helpers\Api\MiniProgramTrait;

class PublishTemplateService {
    use MiniProgramTrait;
    private $openid;
    private $v = '1';

    private function _send($data) {
        $app = $this->miniProgram();
        \Log::info(var_export($data, true));
        $list = $app->subscribe_message->send($data);
        \Log::info(var_export($list, true));
    }

    public function __construct()
    {
        if (env('WECHAT_MINI_PROGRAM_APPID') == 'wxd6dad807c110a938') {
            $this->v = '2';
        }
    }

    public function appoint($openid, $order_sn, $front_num) {
        $method = '_appoint_v' . $this->v;
        $this->$method($openid, $order_sn, $front_num);
    }

    public function _appoint_v1($openid, $order_sn, $front_num) {
        $data = [
            'template_id' => 'eeNPWI878cb2nscMQxWd8yUdZS6UYC3LO-iJi7MmbsM',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'character_string4' => [
                    'value' => $order_sn,
                ],
                'thing5' => [
                    'value' => '排队成功',
                ],
                'thing6' => [
                    'value' => $front_num . "辆车"
                ]
            ],
        ];
        $this->_send($data);
    }

    public function _appoint_v2($openid, $order_sn, $front_num) {
        $data = [
            'template_id' => 'MIT8mEzNRqwse4SU36tphlwW9UyRRtdlgu7wmtSZEBQ',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'character_string4' => [
                    'value' => $order_sn,
                ],
                'thing5' => [
                    'value' => '排队成功',
                ],
                'thing6' => [
                    'value' => $front_num . "辆车"
                ]
            ],
        ];
        $this->_send($data);
    }


    public function signIn($openid, $title) {
        $method = '_signIn_v' . $this->v;
        $this->$method($openid, $title);
    }

    public function _signIn_v1($openid, $title) {
        $data = [
            'template_id' => 'W6gLRtcRWZfC0l6U_5nNbVNu8rTvC8hcTC2kYQrRaYU',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'thing1' => [
                    'value' => $title,
                ],
                'thing2' => [
                    'value' => '签到成功',
                ],
                'time3' => [
                    'value' => date('Y-m-d H:i:s')
                ]
            ],
        ];
        $this->_send($data);
    }

    public function _signIn_v2($openid, $title) {
        $data = [
            'template_id' => 'B5vlHGoyKQiCbJNiUZLidrivFuUGd6OAyezbpN2I2ro',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'thing1' => [
                    'value' => $title,
                ],
                'thing2' => [
                    'value' => '签到成功',
                ],
                'time3' => [
                    'value' => date('Y-m-d H:i:s')
                ]
            ],
        ];
        $this->_send($data);
    }

    public function signOut($openid, $title) {
//        $data = [
//            'template_id' => '4xEU2JYuFDdnQKot-nWBJYWZbOkuZ72nQvywwpUpJkA',
//            'touser' => $openid,
//            'page' => '/pages/index/index',
//            'data' => [
//                'thing1' => [
//                    'value' => $title,
//                ],
//                'time2' => [
//                    'value' => date('Y-m-d H:i:s')
//                ]
//            ],
//        ];
//        $this->_send($data);
    }

    public function callSignIn($openid, $car_num, $order_sn) {
        $method = '_callSignIn_v' . $this->v;
        $this->$method($openid, $car_num, $order_sn);
    }

    public function _callSignIn_v1($openid, $car_num, $order_sn) {
        $data = [
            'template_id' => 'Gq94MGf3Li0jHQ2ELESA7D-wh15LFBstBkhFoCNZQrk',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'character_string1' => [
                    'value' => $order_sn,
                ],
                'thing5' => [
                    'value' => $car_num,
                ],
                'character_string7' => [
                    'value' => $order_sn,
                ],
                'thing6' => [
                    'value' => '请进场'
                ],
            ],
        ];
        $this->_send($data);
    }

    public function _callSignIn_v2($openid, $car_num, $order_sn) {
        $data = [
            'template_id' => '49KigwvA9wtRjLFzSBNsFeYlYb4DgXz2MSUZIT5z5Yw',
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'character_string1' => [
                    'value' => $order_sn,
                ],
                'thing5' => [
                    'value' => $car_num,
                ],
                'character_string7' => [
                    'value' => $order_sn,
                ],
                'thing6' => [
                    'value' => '请进场'
                ],
            ],
        ];
        $this->_send($data);
    }

}
